package com.eshop.web.controllers.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.eshop.common.constant.CoreConstant;
import com.eshop.common.params.GoodsParams;
import com.eshop.frameworks.core.controller.BaseController;
import com.eshop.frameworks.core.entity.PageEntity;
import com.eshop.model.manager.User;
import com.eshop.model.mongodb.EUser;
import com.eshop.model.mongodb.ShopAndGoods;
import com.eshop.model.mongodb.SuperGoods;
import com.eshop.service.mongodb.ShopAndGoodsService;
import com.eshop.service.mongodb.SuperGoodsService;

@Controller("web_goods")
//@RequestMapping("/euser")
@RequestMapping("/goods")
public class GoodsController extends BaseController{
	private static final Logger logger = Logger.getLogger(GoodsController.class);
	@Autowired
	private ShopAndGoodsService shopAndGoodsService;
	@Autowired
	private SuperGoodsService superGoodsService;
//	@RequestMapping("/addGoods")
	@RequestMapping("/add")
	public ModelAndView goAddGoods(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/euser/addGoods.httl");
		setVar(mav);
		EUser user = (EUser) this.getSessionAttribute(request,
				CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			mav.setViewName("redirect:/login");
			return mav;
		}
		mav.addObject("user", user);
		mav.addObject("leaf", "add");
		return mav;
	}
	
//	@RequestMapping("/goodsManage")
	@RequestMapping("/manage")
	public ModelAndView goodsManage(HttpServletRequest request,
			HttpServletResponse response, ShopAndGoods query,
			@ModelAttribute("page") PageEntity page) {
		ModelAndView mav = new ModelAndView("/euser/goodsManage.httl");
		setVar(mav);
		try {
			EUser user = (EUser) this.getSessionAttribute(request,
					CoreConstant.USER_SESSION_NAME);
			if (user == null) {
				mav.setViewName("login.httl");
				return mav;
			}
			mav.addObject("user", user);
			this.setPage(page);
			this.getPage().setPageSize(20);
			if (query == null) {
				query = new ShopAndGoods();
			}
			List<ShopAndGoods> list = shopAndGoodsService.getShopperGoods(
					user.getId(), query, page);
			mav.addObject("query", query);
			mav.addObject("goodsList", list);
			mav.addObject("page", this.getPage());
			mav.addObject("leaf", "manage");
		} catch (Exception e) {
			logger.error("GoodsController.goodsManage", e);
		}
		return mav;
	}
	
	@ResponseBody
//	@RequestMapping("/getGoods")
	@RequestMapping("/get")
	public List<SuperGoods> getGoods(SuperGoods query,HttpServletRequest request, HttpServletResponse response,@ModelAttribute("page") PageEntity page) {
		EUser user = (EUser) this.getSessionAttribute(request,
				CoreConstant.USER_SESSION_NAME);
		List<SuperGoods> list = new ArrayList<SuperGoods>();
		try {
			this.setPage(page);
			this.getPage().setPageSize(20);
			if (query == null) {
				query = new SuperGoods();
			}
			query.setUserId(user.getId());
			list = superGoodsService.getGoodsPage(query, page);
		} catch (Exception e) {
			logger.error("GoodsController.listAll", e);
		}

		return list;
	}
	
	@ResponseBody
//	@RequestMapping(value = "/changeOutPrice", method = RequestMethod.POST)
	@RequestMapping(value = "/change", method = RequestMethod.POST)
	public String changeOutPrice(@RequestBody ShopAndGoods shopAndGoods,HttpServletRequest request, HttpServletResponse response) {
		try {
			shopAndGoodsService.changeOutPrice(shopAndGoods);
		} catch (Exception e) {
			logger.error("GoodsController.changeOutPrice", e);
			return "FAILE";
		}
		return "SUCCESS";
	}

	@RequestMapping("/batchInSale")
	public ModelAndView batchInSale(String ids, HttpServletRequest request,User query, @ModelAttribute("page") PageEntity page) {
		String[] idArray = ids.split(",");
		 ModelMap mmap = new ModelMap();  
		 mmap.addAttribute("page", page);
		try {
			shopAndGoodsService.batchInSale(idArray);
		} catch (Exception e) {
			logger.error("GoodsController.batchInSale", e);
		}
		return new ModelAndView("redirect:/goods/manage",mmap);
	}

	@RequestMapping("/batchOutSale")
	public ModelAndView batchOutSale(String ids, HttpServletRequest request,User query, @ModelAttribute("page") PageEntity page) {
		ModelAndView mav = new ModelAndView("/goods/manage");
		String[] idArray = ids.split(",");
		try {
			shopAndGoodsService.batchOutSale(idArray);
			mav.addObject("query", query);
			mav.addObject("page", this.getPage());
		} catch (Exception e) {
			logger.error("GoodsController.batchInSale", e);
		}
		return mav;
	}
	
	@ResponseBody
//	@RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public String insertBatch(@RequestBody GoodsParams goodsParams,
			HttpServletRequest request, HttpServletResponse response) {
		List<ShopAndGoods> sgoods = goodsParams.getSgoods();
		try {
			if (sgoods.size() > 0) {
				shopAndGoodsService.insertBatch(sgoods, ShopAndGoods.class);
			}
		} catch (Exception e) {
			logger.error("GoodsController.listAll", e);
			return "FAILE";
		}
		return "SUCCESS";
	}
}
